<?php
/**
 * @author Nguyen Thanh Tung
 * @copyright 2014
 * @description: save action sample
 */
// not login
// not perm add
if( !isset($AppUI)||
!isset($_SESSION['playersquad'])||
isset($_SESSION['playersquad'])&& ($_SESSION['playersquad']&1<<1)==0)
{
	/*|| 
	(isset($AppUI->id) && !in_array($AppUI->type, array(ROLE_ROOT, ROLE_ADMINISTRATOR)))*/
	redirect(HOST_BACKEND);
}
// data input
$strNational          =  $this->input->post('pnationality',null);
$intLeague            = (int)$this->input->post('pleague',0); 
$strClubid            =  $this->input->post('pclubid',null);			
$intSquad             =  $this->input->post('psquad',0);
$intFrom              = (int)$this->input->post('pfrom', 0);
$intTo   			  = (int)$this->input->post('pto', 0);
$intMatchs 			  =  $this->input->post('pmatchs', 0);
$intMatchs_t 		  =  $this->input->post('pmatchs_t', 0);
$intYellow_c  		  =  $this->input->post('pyellow_c', 0);
$intRed_c      		  =  $this->input->post('pred_c', 0);
$intGoals  			  =  $this->input->post('pgoals', 0);
$intOgs  			  =  $this->input->post('pogs');test($intOgs);
$arrErr               = array();
/*
// validate manual	
//+ National	
if(!isset($strNational) || empty($strNational))
    array_push($arrErr, $arrLocale['select_national_required']);
//+ League	
if(!isset($intLeague) || empty($intLeague))
    array_push($arrErr, $arrLocale['select_league_required']);
//+ Clubid	
if(!isset($strClubid) || empty($strClubid))
    array_push($arrErr, $arrLocale['select_club_required']);
//+ intSquad	
if(isset($intSquad) || !isInt($intSquad))
    array_push($arrErr, $arrLocale['is_not_number']);
//+ intFrom	
if(!isset($intFrom) || empty($intFrom))
    array_push($arrErr, $arrLocale['select_from_required']);
//+ intTo	
if(!isset($intTo) || empty($intTo))
    array_push($arrErr, $arrLocale['select_to_required']);
//+ intFrom <	 intTo
if(intval($intFrom) >= intval($intTo))
    array_push($arrErr, $arrLocale['comparison']);	
//+ intMatchs	
if(isset($intMatchs) || !isInt($intMatchs))
    array_push($arrErr, $arrLocale['is_not_number_match']);
//+ intMatchs_t	
if(isset($intMatchs_t) || !isInt($intMatchs_t))
    array_push($arrErr, $arrLocale['is_not_number_match_t']);
//+ intYellow_c	
if(isset($intYellow_c) || !isInt($intYellow_c))
    array_push($arrErr, $arrLocale['is_not_number_yellow_c']);
elseif(!empty($intYellow_c)&&!is_numeric($intYellow_c))
    array_push($arrErr, $arrLocale['is_not_number_yellow_c']);
//+ intRed_c	
if(isset($intRed_c) || !isInt($intRed_c))
    array_push($arrErr, $arrLocale['is_not_number_red_c']);
//+ intGoals	
if(isset($intGoals) || !isInt($intGoals))
    array_push($arrErr, $arrLocale['is_not_number_goals']);*/	
//+ intOgs
/*if(!isInt($intOgs))
    array_push($arrErr, $arrLocale['is_not_number_ogs']);
*/	

if((int)$intOgs=="")
    array_push($arrErr, $arrLocale['ogs_required']);

/*function isInt($int){
        // First check if it's a numeric value as either a string or number
        if(is_numeric($int) === TRUE){
            // It's a number, but it has to be an integer
            if((int)$int == $int){
                return TRUE;
            // It's a number, but not an integer, so we fail
            }else{
                return FALSE;
            }
        // Not a number
        }else{
            return FALSE;
        }
}	*/

test($arrErr);

if(isset($arrErr) && count($arrErr)==0) {//allow save
    // unset session if exists
	
    if(isset($_SESSION['PLAYER_ERROR'])) unset($_SESSION['PLAYER_ERROR']);
    if(isset($_SESSION['PLAYER_POST'])) unset($_SESSION['PLAYER_POST']);
    $this->load->model('uniform_model', 'uniform');	
	
    $intError = $this->uniform->add($AppUI->id,$intClubId, $intFrom, $intTo, $intHomeShirt, $strShort, $strDescription);
    if($intError==0) CI_Function::setMsg($arrMessage['save_add']);
    elseif($intError==1) array_push($arrErr, $arrLocale['noperm_save']);
}
// occur error

if (isset($arrErr) && count($arrErr) > 0) {
    $_SESSION['PLAYER_ERROR']    = $arrErr;    
    $_SESSION['PLAYER_POST']     = $this->input->post();
    
    redirect(HOST_BACKEND . 'playersquad/add');
}
redirect(HOST_BACKEND.'playersquad');
